﻿using System;
using System.Collections.Generic;
using System.Text;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Collections.Specialized;

namespace Duson.Controls
{
    [ToolboxData(@"<{0}:TestControl runat='server'></{0}:TestControl>")
    ]
    public class TestControl : WebControl, IPostBackEventHandler//, IPostBackDataHandler
    {
        #region 属性
        [Browsable(true),
        Category("uer"),
        Description("rrrrrrr"),
        DefaultValue("444")
        ]
        public string Str
        {
            get { return ViewState["Str"] != null ? ViewState["Str"].ToString() : ""; }
            set { ViewState["Str"] = value; }
        }
        #endregion

        #region Click事件
        // 声明Click事件委托
        private static readonly object ClickKey = new object();
        // 声明Click事件委托
        public event EventHandler Click
        {
            add
            {
                Events.AddHandler(ClickKey, value);
            }
            remove
            {
                Events.RemoveHandler(ClickKey, value);
            }
        }

        // 定义OnClick事件处理程序
        protected virtual void OnClick(EventArgs e)
        {
            EventHandler clickEventDelegate = (EventHandler)Events[ClickKey];
            if (clickEventDelegate != null)
            {
                clickEventDelegate(this, e);
            }
        }

        // 实现RaisePostBackEvent方法,处理回发事件
        public void RaisePostBackEvent(string eventArgument)
        {
            OnClick(new EventArgs());
        }
        #endregion

        #region TextChanged事件
        //public event EventHandler TextChanged;

        //public bool LoadPostData(string postDataKey, NameValueCollection postCollection)
        //{
        //    string postedValue = postCollection[postDataKey];
        //    //检查新旧数据
        //    if (!Str.Equals(postedValue))
        //    {
        //        Str = postedValue;
        //        return true;
        //        //自动调用RaisePostDataChangedEvent()
        //    }
        //    else
        //        return false;
        //    //不发生变化
        //}

        //public void RaisePostDataChangedEvent()
        //{
        //    OnTextChanged(new EventArgs());
        //}

        //protected virtual void OnTextChanged(EventArgs e)
        //{
        //    if (TextChanged != null)
        //        TextChanged(this, e);
        //}
        #endregion

        #region 输出内容
        protected override HtmlTextWriterTag TagKey
        {
            get { return HtmlTextWriterTag.Div; }
        }

        protected override void AddAttributesToRender(HtmlTextWriter writer)
        {
            //为table标签定义属性和样式
            writer.AddStyleAttribute(HtmlTextWriterStyle.BorderColor, "#ccc");
            writer.AddStyleAttribute(HtmlTextWriterStyle.BorderWidth, "1px");
            writer.AddStyleAttribute(HtmlTextWriterStyle.BorderStyle, "solid");
            writer.AddStyleAttribute(HtmlTextWriterStyle.Width, "400px");
            writer.AddStyleAttribute(HtmlTextWriterStyle.Height, "400px");

            base.AddAttributesToRender(writer);

        }

        protected override void RenderContents(HtmlTextWriter writer)
        {
            writer.AddAttribute(HtmlTextWriterAttribute.Value, "确定");
            writer.AddAttribute(HtmlTextWriterAttribute.Type, "submit");
            writer.AddAttribute(HtmlTextWriterAttribute.Name, this.UniqueID);
            writer.RenderBeginTag(HtmlTextWriterTag.Input);
            writer.RenderEndTag();

            /*
            writer.Write("<A href=\"");
            writer.Write(Page.GetPostBackClientHyperlink(this, ""));
            writer.Write("\">" + Str + "</A>");*/

            /*
            Page.VerifyRenderingInServerForm(this);
            writer.Write("<INPUT type=\"text\" name=\"");
            writer.Write(this.UniqueID);
            writer.Write("\" value=\"" + this.Str + "\" />");*/

        }
        #endregion
    }
}
